using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
using THSLib;
namespace THSERP.BusinessEntities
{
   public class BusinessControllerFactory
    {
       public static BaseBusinessController GetBusinessController(String strBusinessControllerName)
       {


           try
           {
               Assembly THSBusinessEntities = Assembly.GetExecutingAssembly();
               Type businessControllerType = THSBusinessEntities.GetType("THSERP.BusinessEntities." + strBusinessControllerName);
               THSUtil.countInvoke++;
               return (BaseBusinessController)THSLib.DynamicInvoker.DynamicCreateInstanceObject(businessControllerType);
           }
           catch (Exception)
           {
               Assembly THSLibas = Assembly.LoadFrom(Application.StartupPath + @"\THSLib.dll");
               Type businessControllerType = THSLibas.GetType("THSLib." + strBusinessControllerName);
               THSUtil.countInvoke++;
               if (businessControllerType != null)
                   return (BaseBusinessController)THSLib.DynamicInvoker.DynamicCreateInstanceObject(businessControllerType);
               else
               {
                   Assembly erpAssemply = Assembly.LoadFrom(Application.StartupPath + @"\THSERP.exe");
                   businessControllerType = erpAssemply.GetType("THSERP." + strBusinessControllerName);
                   if (businessControllerType != null)
                       return (BaseBusinessController)THSLib.DynamicInvoker.DynamicCreateInstanceObject(businessControllerType);
                   else
                        return null;
               }
           }

       }
       

        public static Type GetBusinessControllerType(String strBusinessControllerName)
        {
            Assembly a = Assembly.GetExecutingAssembly();
            try
            {
                BaseBusinessController objController = GetBusinessController(strBusinessControllerName);
                if (objController != null)
                    return objController.GetType();
                else
                {
                    Assembly THSLib = Assembly.LoadFrom(Application.StartupPath + @"\THSLib.dll");
                    return a.GetType("THSLib." + strBusinessControllerName);
                }
            }
            catch (Exception)
            {
                Assembly THSLib = Assembly.LoadFrom(Application.StartupPath + @"\THSLib.dll");
                return THSLib.GetType("THSLib." + strBusinessControllerName);
            }
        }
    }
}
